//
//  HUTabBarView.swift
//  weiboProject
//
//  Created by hehuan on 16/7/18.
//  Copyright © 2016年 hehuan. All rights reserved.
//

import UIKit

class HUTabBarView: UITabBar {

    var composeCallBack:(() -> Void)?
    
    lazy var composeButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(button)
        
        button.addTarget(self, action: "composeCilck", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    func composeCilck() {
        
        composeCallBack?()
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        var index = 0
        
        let newItemWidth = self.frame.width / 5
        
        for item in self.subviews{
            
            
            
            if item.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                item.frame = CGRect(x: CGFloat(index) * newItemWidth, y: 0, width: newItemWidth, height: self.frame.height)
                
                index++
                
                if index==2 {
                    index++
                }
            }
        }
        
        composeButton.frame = CGRect(x: 2 * newItemWidth , y: 0, width: newItemWidth, height: self.frame.height)
    }
    

}
